﻿$(function(){//延迟加载
	var dvbelong='';
	var ipresId='';
	var done=0;
	var newfuncrows=null;
	var deletefuncrows=null;
	var uprows=null;
	var upi=0;
	var checkrowsbefore=null;
	
	var editIndex = undefined;	
	
	$('a[name=dvres]').click(function(){
		$('#p').height('700px');
		dvbelong=$(this).attr('value');
		$('#searchres').datagrid('reload',{
			dvbelong:dvbelong
		});
		$('#tool1').show();
	});
	
	
	$('#searchres').datagrid({
		width:'auto',
		height:'auto',
		//striped:true,	
//		toolbar:'#tb1',
		url:'showvlanrules',
		queryParams:{
			dvbelong:dvbelong,
		},
		method:'get',
		pagination:true,
		rownumbers:true,
		toolbar: [{
			iconCls: 'icon-add',
			text:'规则新增',
			handler: function(){
				append();
				}
		},'-',{
			iconCls: 'icon-save',
			text:'确认修改',
			handler: function(){
				accept();
			}
		},'-',{
			iconCls: 'icon-undo',
			text:'取消修改',
			handler: function(){
				reject();
			}
		},'-',{
			iconCls: 'icon-remove',
			text:'删除规则',
			handler: function(){
				removeit();
			}
		}],
		columns:
		[[{
			field:'ck',
			checkbox:true
		},
		{"field":"vrname",
		"title":"规则名称",
		"width":"12%",
		editor:{
      	  type:'textbox',
      		 options:{		        			 
      			 required:true 
      		 }
        }
		},
		{
		"field":"dvbelong",
		"title":"规则设备类型",
		"width":"8%"
		},
		{
		"field":"vlanbegin",
		"title":"pppvlan起始",
		"width":"10%",
		editor:{
      	  type:'numberbox',
      		 options:{		        			 
      			 required:true 
      		 }
        }
		},
		{
		"field":"vlanend",
		"title":"pppvlan终止",
		"width":"10%",
		editor:{
      	  type:'numberbox',
      		 options:{		        			 
      			 required:true 
      		 }
        }
		},
		{
		"field":"iptvvlan",
		"title":"iptvvlan",
		"width":"10%",
		editor:{
	      	  type:'numberbox',
	      		 options:{		        			 
	      			 required:true 
	      		 }
	        }
		},
		{
		"field":"voipvlan",
		"title":"voipvlan",
		"width":"10%",
		editor:{
	      	  type:'numberbox',
	      		 options:{		        			 
	      			 required:true 
	      		 }
	        }
		},
		{
		"field":"ylvlan",
		"title":"ylvlan",
		"width":"10%",
		editor:{
	      	  type:'numberbox',
	      		 options:{		        			 
	      			 required:true 
	      		 }
	        }
		},
		{
		"field":"cvlan",
		"title":"cvlan",
		"width":"10%",
		editor:{
	      	  type:'numberbox',
	      		 options:{		        			 
	      			 required:true 
	      		 }
	        }
		},			
		{
		"field":"itmsvlan",
		"title":"itmsvlan",
		"width":"10%",
		editor:{
	      	  type:'numberbox',
	      		 options:{		        			 
	      			 required:true 
	      		 }
	        }
		},
		{
		"field":"status",
		"title":"规则状态",
		"width":"6%"
		}
		]],
	onClickRow:onClickRow,
	onCheck:function(index,row){

	},
	onUncheck:function(index,row){
//		$(this).datagrid('updateRow',{
//			index: index,
//			row: {
//				status:'禁用'
//			}
//		});
//		if(editIndex == undefined){
//			uprows[index] = row
//			done=1;
//		}
	},
	onLoadSuccess:function(data){
		var rows=data.rows;
		for(var i=0;i<rows.length;i++){
			if(rows[i].status=='启用'){
				$(this).datagrid('checkRow',i);
			}
		}
		checkrowsbefore=$(this).datagrid('getChecked');
	}
	});
	
	$('#compledit').click(function(){
		if(done==0){
			alert('请先确定修改');
			return;
		}
		if(uprows.length==0&&newrows.length==0&&deleterows.length==0){
			alert('未修改');
			return;
		}
//		alert(uprows.length);
//		alert(newrows.length);
//		alert(deleterows.length);
//		return;
		var listupdate=JSON.stringify(uprows);
		var listnew=JSON.stringify(newrows);
		var listdelete=JSON.stringify(deleterows);
		$.ajax({
			url:'updatevlanrules',
			type:'POST',
			data:{
				listupdate:listupdate,
				listnew:listnew,
				listdelete:listdelete
			},
			success:function(data){
				alert(data.msg);
				$('#searchres').datagrid('reload',{
					dvbelong:dvbelong
				});
			}
		});
	});
	
	$('#editselect').click(function(){
		var newIds='';
		var deleteIds='';
		var checkrowsafter=$('#searchres').datagrid('getChecked');
		for(var i=0;i<checkrowsafter.length;i++){//取得现在所选的ID 			
			var flag1=0;
			for(var j=0;j<checkrowsbefore.length;j++){
				if(checkrowsafter[i].vrId==checkrowsbefore[j].vrId){//重复区域
					flag1=1;
//					usersbefore[j]='';
					break;
				}
			}
			if(flag1==1){//有重复区域
				
			}else{//没有重复区域，则需要新增
				newIds=newIds+checkrowsafter[i].vrId+',';
			}
		}
		for(var i=0;i<checkrowsbefore.length;i++){
			var flag1=0;
			for(var j=0;j<checkrowsafter.length;j++){
				if(checkrowsbefore[i].vrId==checkrowsafter[j].vrId){
					flag1=1;
					break;
				}
			}
			if(flag1==1){
				
			}else{//没有重复则需删除
				deleteIds=deleteIds+checkrowsbefore[i].vrId+',';
			}
		}
		if(newIds!='')
			newIds=newIds.substring(0,newIds.length-1);
		if(deleteIds!='')
			deleteIds=deleteIds.substring(0,deleteIds.length-1);
		if(deleteIds==''&&newIds==''){
			alert('未修改!');
			return
		}
		$.ajax({
			url:'editrulescheck',
			type:'post',
			data:{
				newIds:newIds,
				deleteIds:deleteIds
			},
			success:function(data){
				alert(data.msg);
				$('#searchres').datagrid('reload',{
					dvbelong:dvbelong
				});
			}
		});
	});
	
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#searchres').datagrid('validateRow', editIndex)){
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}

	function onClickRow(index){
		done=0;
		if (editIndex!= index){
			if (endEditing()){
				$('#searchres').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#searchres').datagrid('selectRow', editIndex);
			}
		}
	}

	function append(){
		if (endEditing()){
			if(dvbelong==''){
				return
			}
			$('#searchres').datagrid('appendRow',{
				vrname: '',
				dvbelong:dvbelong,
				vlanbegin:0,
				vlanend:0,
				iptvvlan:0,
				voipvlan:0,
				ylvlan:0,
				cvlan:0,
				itmsvlan:0,
				status:'禁用',				
			});

			editIndex = $('#searchres').datagrid('getRows').length-1;
			$('#searchres').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
			}
		done=0;
		}
	
	function removeit(){
		if (editIndex == undefined){return}
		$('#searchres').datagrid('cancelEdit', editIndex).datagrid('deleteRow', editIndex);

	}

	function accept(){
		if (endEditing()){
			done=1;
			uprows = $('#searchres').datagrid('getChanges','updated');
			newrows = $('#searchres').datagrid('getChanges','inserted');
			deleterows=$('#searchres').datagrid('getChanges','deleted');
			$('#searchres').datagrid('acceptChanges');
			editIndex = undefined;
		}
	}

	function reject(){
		$('#funclist').datagrid('rejectChanges');
		editIndex = undefined;
	}

	function getChanges(){
		var rows = $('#funclist').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}
});